<template>
  <div>
        <a-button type="primary" @click="handleLinkAdd">
			添加
		</a-button>
		<a-card>
			<a-table :columns="columns" 
				:data-source="linkList" 
				rowKey="linkId" bordered 
				:pagination="true"
				:loading="loading">
				<template slot="operation" slot-scope="text,record">
					<a-button type="primary" @click="handleLinkEdit(record.linkId)">
						修改
					</a-button>
					<a-popconfirm title="你确定要删除嘛" ok-text="Yes" cancel-text="No"
						@confirm="handleLinkDelete(record.linkId)">
						<a-button type="danger" >
							删除
						</a-button>
					</a-popconfirm>
				</template>
			</a-table>
      </a-card>
  </div>
</template>

<script>
import {
    linkColumns as columns
} from '@/assets/js/tableList/SystemTable.js'
import {
    getLinkListByPage,
    deleteLinkById
} from '@/api/System.js'
export default {
    name: 'LinkList',
    data () {
        return {
            loading: true,
            disable: false,
            columns: [],
            linkList: []
        }
    },
    created() {
        this.initList()
        this.getLinkList()
    },
    methods: {
        // 初始化列表
        initList() {
			this.columns = columns
		},
        // 分页加载友情链接列表
        async getLinkList() {
            const {
                data: res
            } = await getLinkListByPage()
            console.log(res)
            if (res.code !== 20005)
                return this.$message.error('列表加载出错')
            this.linkList = res.data
            this.loading = false
            this.$message.success(res.msg)
        },
        onPageChange(newPage) {
            this.page.currentPage = newPage
            this.getLinkList()
        },
        // 页面变化
        onSizeChange(current, newSize) {
            this.page.pageSize = newSize;
            this.getLinkList()
        },
        // 添加友情链接
        handleLinkAdd () {
            this.$router.push(`/system/link/edit`) 
        },
        // 编辑友情链接
        handleLinkEdit (id) {
            this.$router.push(`/system/link/edit?id=${id}`)
        },
        // 删除友链
        async handleLinkDelete (id) {
            const {data : res} = await deleteLinkById(id)
            if (res.code != 20003) 
                return this.$message.error('删除失败')
            let arr = this.linkList
            arr.forEach((item, index, res) => {
                if (item.linkId == id) {
                    res.splice(index,1)
                }
            })
            this.$message.success(res.msg)
        }
    }
}
</script>

<style>

</style>